package live.jipiao.dao;

import java.io.IOException;
import java.util.List;

import live.common.Format;
import live.common.PropertiesManager;
import live.jipiao.entity.AirQuery;
import live.jipiao.entity.PnrInfo;
import live.jipiao.entity.RateInfos;
import live.jipiao.entity.AirData.FlightItem;


public class FlightCommon {
	private StringBuffer buffer=null;
	private Format format=null;
	/**
	 * 组装航班信息
	 * @return 航班信息串
	 */
	public String returnFlightInfo(AirQuery query,List<FlightItem> chooseFlights){
		FlightItem formFlight=chooseFlights.get(0);
		format = new Format();
		buffer=new StringBuffer(query.getFromCity()).append("^")
		.append(query.getToCity()).append("^").append(format.dateFormatString(formFlight.getDepTime(), "yyyy-MM-dd HH:mm"))
		.append("^").append(format.dateFormatString(formFlight.getArrTime(), "yyyy-MM-dd HH:mm"))
		.append("^").append(formFlight.getCabinInfo().getCode())
		.append("^").append(formFlight.getFlightNo())
		.append("^").append(formFlight.getModel())
		.append("^").append(formFlight.getCabinInfo().getDiscount())
		.append("^").append(0);
		if(query.getVoyageType()==2){
			FlightItem toFlight=chooseFlights.get(0);
			buffer.append("|").append(query.getToCity()).append("^")
			.append(query.getFromCity()).append("^").append(format.dateFormatString(toFlight.getDepTime(), "yyyy-MM-dd HH:mm"))
			.append("^").append(format.dateFormatString(toFlight.getArrTime(), "yyyy-MM-dd HH:mm"))
			.append("^").append(toFlight.getCabinInfo().getCode())
			.append("^").append(toFlight.getFlightNo()).append("^").append(toFlight.getModel())
				.append("^").append(toFlight.getCabinInfo().getDiscount())
				.append("^").append(1);
		}
		return buffer.toString();
	}
	
	public String returnFlightInfoForPnr(AirQuery query,List<FlightItem> chooseFlights){
		FlightItem formFlight=chooseFlights.get(0);
		buffer=new StringBuffer(query.getFromCity()).append("^")
		.append(query.getToCity()).append("^").append(query.getFromDate())
		.append("^").append(formFlight.getCabinInfo().getCode())
		.append("^").append(formFlight.getFlightNo());
		if(query.getVoyageType()==2){
			FlightItem toFlight=chooseFlights.get(0);
			buffer.append("|").append(query.getToCity()).append("^")
			.append(query.getFromCity()).append("^").append(query.getToDate())
			.append("^").append(toFlight.getCabinInfo().getCode())
			.append("^").append(toFlight.getFlightNo());
		}
		return buffer.toString();
	}
	
	/**
	 * 组装乘客信息
	 * @return 乘客信息串
	 */
	/*public String returnPassenagerInfo(){
		
		return null;
	}*/
	
	/**
	 * 组装PNR信息
	 * @return PNR 信息串
	 */
	public String returnPnrInfo(PnrInfo item){
		buffer = new StringBuffer(item.getCode()).append("^")
		.append(item.getBigCode()).append("^").append(
				item.getTeamType()).append("^").append(
						item.getRouteType()).append("^").append(
								item.getFare()).append("^").append(
				Float.parseFloat(item.getTax())
						+ Float.parseFloat(item.getYq()))
		.append("^").append(item.getOfficeNo());
		return buffer.toString();
	}
	
	/**
	 * 组装订单信息
	 * @return 订单信息串
	 * @throws IOException 
	 */
	public String returnOrderInfo(List<RateInfos> rates){
		String partner;
		try {
			partner = (String) PropertiesManager.getInstance().returnPropValue("partner");
			buffer = new StringBuffer();
			RateInfos item =null;
			for (int i = 0; i < rates.size(); i++) {
				item = rates.get(i);
				buffer.append("0^").append(partner).append("^").append(partner)
				.append("^").append(item.getRateNo()).append("^").append(item.getDisCount())
				.append("^").append(0).append("^").append(item.getAdultsType());
				if(i!=rates.size()-1){
					buffer.append("|");
				}
				item = null;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("读取patner信息时出现错误");
		}
		return buffer.toString();
	}
}
